<?php
require_once 'util/class.daofactory.php';

class RelationService {

	public function getFollowee($userId, $pageNum) {
		$result = array();
		if(!$userId || !is_int($userId) || $userId<=0
				|| !$pageNum || !is_int($pageNum) || $pageNum<0) {
			$result['status']=Constants::$ERROR_INVALID_ARGS;
			return $result;
		}
		
		global $relationDao;
		global $userDao;
		if($relationDao==null) {
			$result['status']=Constants::$ERROR_DB;
			return $result;
		}
		
		$feIds = $relationDao->getFollowees($userId);
		$len=count($feIds);
		if($len==0 || $len<($pageNum-1)*Constants::$FRIEND_PAGE_SIZE) {
			$result['status']=Constants::$SUCCESS;
			$result['data']=array('totalNum'=>$len, 'users'=>array());
			return $result;
		}
		
		$start = ($pageNum-1)*Constants::$FRIEND_PAGE_SIZE;
		$offset = Constants::$FRIEND_PAGE_SIZE;
		if($pageNum*Constants::$FRIEND_PAGE_SIZE>$len)
			$offset=$len-$start;
		
		$matchIds = array_slice($feIds, $start, $offset);
		$users = array();
		$user=null;
		foreach ($matchIds as $id) {
			$user=$userDao->getUserBasicInfo($id);
			if($user==null)
				continue;
			$users[]=$user;
		}
		
		$result['status']=Constants::$SUCCESS;
		$result['data']=array('totalNum'=>$len, 'users'=>$users);
		return $result;
	}
	
	public function getFollower($userId, $pageNum) {
		$result = array();
		if(!$userId || !is_int($userId) || $userId<=0
				|| !$pageNum || !is_int($pageNum) || $pageNum<0) {
			$result['status']=Constants::$ERROR_INVALID_ARGS;
			return $result;
		}
		
		global $relationDao;
		global $userDao;
		
		if($relationDao==null) {
			$result['status']=Constants::$ERROR_DB;
			return $result;
		}
		
		$flIds = $relationDao->getFollowers($userId);
		$len=count($flIds);
		if($len==0 || $len<($pageNum-1)*Constants::$FRIEND_PAGE_SIZE) {
			$result['status']=Constants::$SUCCESS;
			$result['data']=array('totalNum'=>$len, 'users'=>array());
			return $result;
		}
		
		$start = ($pageNum-1)*Constants::$FRIEND_PAGE_SIZE;
		$offset = Constants::$FRIEND_PAGE_SIZE;
		if($pageNum*Constants::$FRIEND_PAGE_SIZE>$len)
			$offset=$len-$start;
		
		$matchIds = array_slice($flIds, $start, $offset);
		$users = array();
		$user=null;
		foreach ($matchIds as $id) {
			$user=$userDao->getUserBasicInfo($id);
			if($user==null)
				continue;
			$users[]=$user;
		}
		
		$result['status']=Constants::$SUCCESS;
		$result['data']=array('totalNum'=>$len, 'users'=>$users);
		return $result;
	}
	
	public function createFollowRelation($followeeId, $followerId) {
		$result = array();
		if(!$followeeId || !is_int($followeeId) || $followeeId<=0 
			|| !$followerId || !is_int($followerId) || $followerId<=0) {
			$result['status']=Constants::$ERROR_INVALID_ARGS;
			return $result;
		}
		
		// step1, add relation
		global $relationDao;
		$status = $relationDao->addRelation($followeeId, $followerId);
		if($status!=Constants::$SUCCESS) {
			$result['status']=$status;
			return;
		}
		// step2, add notify
		global $notifyDao;
		$notifyDao->addNotify($followerId, array('followee'=>1));
		$result['status']=Constants::$SUCCESS;
		$result['data']=array();
		return $result;
	}
	
	public function destoryFollowRelation($followeeId, $followerId) {
		$result = array();
		if(!$followeeId || !is_int($followeeId) || $followeeId<=0
				|| !$followerId || !is_int($followerId) || $followerId<=0) {
			$result['status']=Constants::$ERROR_INVALID_ARGS;
			return $result;
		}
		
		global $relationDao;
		$result['status']=$relationDao->removeRelation($followeeId, $followerId);
		$result['data']='';
		return $result;
	}
}